package control;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.util.ArrayList;


/**
 * InFile
 * @author Adriano de Albuquerque Batista
 * @author Alfeu Buriti
 * @author Pablo Diego Alencar
 * @author Yuri Cariry Cabral de Melo
 *
 */
public class InFile {

	/**
	 * Construtor InFile vazio 
	 */
	public  InFile (){
	}


	/**
	 * File reader
	 * @param pathname pathname
	 * @return s2 s2 conteudo do arquivo pathname 
	 */
	public String readFile (String pathname){//String constructor
		System.out.println("Inicio do InFile -> readFile");	

		String s2 = new String();
		BufferedReader bf=null;        
		try{
			try {
				// 1. Buffered input file 
				bf= new BufferedReader(
						new InputStreamReader(new FileInputStream(pathname), "UTF-8"));
				s2=getText (bf);
				//bf.close();
			} finally{
				if (bf != null){
					bf.close();
				}
			}
		}catch(FileNotFoundException e) {
			System.out.println("File Not Found:" + pathname);
		}catch(IOException e) {
			System.out.println("IO Exception");
		}          
		System.out.println("Fim do InFile -> readFile");	
		return s2;
	}



	/**
	 * InFileArray
	 * @param pathname
	 * @return lineArray lineArray
	 */
	public String[] inFileArray (String pathname){
		System.out.println("Inicio do InFile -> inFileArray");	

		ArrayList<String> lines= new ArrayList<String> ();
		try {
			File in= new File(pathname);
			BufferedReader bf= new BufferedReader(new InputStreamReader(new FileInputStream(in), "UTF-8"));
			String s;
			while((s = bf.readLine())!= null){
				lines.add(s);
			}
			bf.close();
		}catch(FileNotFoundException e) {
			System.out.println("File Not Found:" + pathname);
		}catch(IOException e) {
			System.out.println("IO Exception");
		}    
		// Convert ArrayList to an array of Strings
		String[] lineArray = lines.toArray(new String[0]);

		System.out.println("Fim do InFile -> inFileArray");
		return lineArray;
	}




	/**
	 * getText
	 * @param reader
	 * @return resultado
	 */
	public String getText (Reader reader) {//Read file to StringBuffer
		//StringWriter s2 = new StringWriter();
		System.out.println("Inicio do InFile -> getText");	

		int n;
		int isize = 512;
		StringWriter sw= new StringWriter();

		try{
			char[] buf = new char[8192];
			sw = new StringWriter(isize);
			while ((n = reader.read(buf, 0, buf.length)) != -1){
				sw.write(buf, 0, n);
			}
		}
		catch(IOException e) {
			System.out.println("IO Exception");
		}  

		System.out.println("Fim  do InFile -> getText");
		String resultado = sw.toString(); 
		return resultado;
	}
	/**
	 * getText BufferedReader
	 * @param reader
	 * @return resultado
	 */
	public String getText (BufferedReader reader) {//Read file to StringBuffer

		System.out.println("Inicio do InFile -> getText");	
		int n;
		int isize = 512;
		StringWriter sw = new StringWriter();

		try{
			char[] buf = new char[8192];
			sw = new StringWriter(isize);
			while ((n = reader.read(buf, 0, buf.length)) != -1){
				sw.write(buf, 0, n);
			}
		}
		catch(IOException e) {
			System.out.println("IO Exception");
		}     
		System.out.println("Fim do InFile -> getText");	
		String resultado = sw.toString(); 
		return resultado;
	}



	/**
	 * getImage
	 * @param in InputStream
	 * @param filename String
	 */
	public void getImage(InputStream in, String filename){
		System.out.println("Inicio do InFile -> filename");

		try {
			//PNG created on MAC may have a 16-byte prefix which prevents successful reading.
			//Just cut it off!.
			BufferedImage bi = ImageIO.read(in);            
			ImageIO.write(bi, "PNG", new File(filename));
		} catch (IOException e){
			throw new RuntimeException(e);
		}         
		System.out.println("Fim do InFile -> filename");	
	}

}
